package com.tl.sboot.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tl.sboot.mapper.RedisMapper;
import com.tl.sboot.service.RedisService;
import com.tl.sboot.utils.CSVUtils;
import com.tl.sboot.utils.RedisUtil;
/**
 * 2016年12月9日17:15:44 陈君文
 * redisService
 * @author Administrator
 *
 */
@Service
public class RedisServiceImpl implements RedisService{
	@Autowired
	RedisMapper redisMapper;
	@Autowired
	RedisUtil ru;
	@Override
	public Map<String, String> findSTK_COM_PROFILE() {
		List<Map<String, Object>> scpListMap = redisMapper.findSTK_COM_PROFILE();
		for (Map<String, Object> map : scpListMap) {
				String REGION = (String) map.get("REGION");
				Integer STOCKCODE = (Integer) map.get("STOCKCODE");
				ru.sadd("SE.REGION."+REGION, STOCKCODE+"");
				System.out.println(map);
		}
		return null;
	}
	
	@Override
	public Map<String, Long> findSTK_CASH_GENC_Count() {
		String sql = "select ";
		List<Map<String,String>> fields = redisMapper.findSTK_CASH_GENField();
		for (int i = 0; i < fields.size(); i++) {
			Map<String, String> map = fields.get(i);
			String field = map.get("COLUMN_NAME");
			if (fields.size()-1!=i) {
				sql += "count("+field+"),";
			}else {
				sql += "count("+field+")";
			}
		}
		sql += "FROM STK_CASH_GEN where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00'";
		Map<String, Long> fieldCount = redisMapper.findSTK_CASH_GENC_Count(sql);
		List<String> csv = new ArrayList<>();
		csv.add("列名,个数");
		Set<String> keySet = fieldCount.keySet();
		for (String key : keySet) {
			csv.add(key+","+fieldCount.get(key));
		}
		boolean isSuccess=CSVUtils.exportCsv(new File("D:/巨灵数据统计/STK_CASH_GEN-现金流量表.csv"), csv);
		if (isSuccess) {
			System.out.println("生成文件成功");
		}
		return fieldCount;
	}

	@Override
	public Map<String, Long> findSTK_INCOME_GENP_Count() {
		String sql = "select ";
		List<Map<String,String>> fields = redisMapper.findSTK_INCOME_GENField();
		for (int i = 0; i < fields.size(); i++) {
			Map<String, String> map = fields.get(i);
			String field = map.get("COLUMN_NAME");
			if (fields.size()-1!=i) {
				sql += "count("+field+"),";
			}else {
				sql += "count("+field+")";
			}
		}
		sql += "FROM STK_INCOME_GEN where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00'";
		Map<String, Long> fieldCount = redisMapper.findSTK_INCOME_GENP_Count(sql);
		List<String> csv = new ArrayList<>();
		csv.add("列名,个数");
		Set<String> keySet = fieldCount.keySet();
		for (String key : keySet) {
			csv.add(key+","+fieldCount.get(key));
		}
		boolean isSuccess=CSVUtils.exportCsv(new File("D:/巨灵数据统计/STK_INCOME_GEN-利润表（通用）.csv"), csv);
		if (isSuccess) {
			System.out.println("生成文件成功");
		}
		return fieldCount;
	}

	@Override
	public Map<String, Long> findSTK_BALA_GENB_Count() {
		String sql = "select ";
		List<Map<String,String>> fields = redisMapper.findSTK_BALA_GENField();
		for (int i = 0; i < fields.size(); i++) {
			Map<String, String> map = fields.get(i);
			String field = map.get("COLUMN_NAME");
			if (fields.size()-1!=i) {
				sql += "count("+field+"),";
			}else {
				sql += "count("+field+")";
			}
		}
		sql += "FROM STK_BALA_GEN where RPT_SRC='年报' and RPT_TYPE='合并' and ENDDATE='2015-12-31 00:00:00'";
		Map<String, Long> fieldCount = redisMapper.findSTK_BALA_GENB_Count(sql);
		List<String> csv = new ArrayList<>();
		csv.add("列名,个数");
		Set<String> keySet = fieldCount.keySet();
		for (String key : keySet) {
			csv.add(key+","+fieldCount.get(key));
		}
		boolean isSuccess=CSVUtils.exportCsv(new File("D:/巨灵数据统计/STK_BALA_GEN-资产负债表.csv"), csv);
		if (isSuccess) {
			System.out.println("生成文件成功");
		}
		return fieldCount;
	}
	
	
	
}
